package com.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.student.bean.Student;
import com.student.dao.Dao;

public class StudentEdit extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(StudentEdit.class);
	private Dao dao;
       
    public StudentEdit() {
        super();
        dao = new Dao();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/*
	 * 获取用户信息，进入编辑页面
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info("User is about to enter the edit page.");
		
		String id = request.getParameter("id");
		PrintWriter pw = response.getWriter();
		if ( "".equals(id) || id == null ) {
			pw.println("<script>alert('操作异常！');window.location.href='StudentList';</script>");
			pw.flush();
			pw.close();
		} else {
			Student stu = dao.getStudentById(Integer.parseInt(id));
			if ( stu == null ) {
				pw.println("<script>alert('不存在该学生！');window.location.href='StudentList';</script>");
				pw.flush();
				pw.close();
			} else {
				request.setAttribute("student", stu);
				request.getRequestDispatcher("studentedit.jsp").forward(request, response);
			}
		}
	}

}
